package com.hboxs.asl.controller.app.product;

import com.hboxs.asl.JsonMessage;
import com.hboxs.asl.controller.app.BaseController;
import com.hboxs.asl.entity.ProductCategory;
import com.hboxs.asl.service.ProductCategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xxx on 11/23//2016.
 * 电商接口
 */
@Controller(value = "appProductCategoryController")
@RequestMapping(value = "/app/pc")
public class ProductCategoryController extends BaseController {

    @Resource(name = "productCategoryServiceImpl")
    private ProductCategoryService productCategoryService;

    /**
     * 分类roots
     *
     * @return json
     */
    @RequestMapping(value = "/roots", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage roots() {
        List<ProductCategory> roots = productCategoryService.findRoots();
        if (roots == null) {
            return JsonMessage.error("找不到该商品分类");
        }
        return JsonMessage.success("roots", JsonMessage.optimiseObj(roots, "name", "image", "id"));

    }


    /**
     * 分类roots  - 下级分类
     *
     * @return json
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage list(Long pcId) {
        ProductCategory productCategory = productCategoryService.find(pcId);
        if (productCategory == null) {
            return JsonMessage.error("找不到该商品分类");
        }
        return JsonMessage.success("roots", JsonMessage.optimiseObj(productCategory.getChildren(), "name", "id"));
    }

}
